package com.zygh.lz.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.dao.InformationMapper;
import com.zygh.lz.dao.InformationUserMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Information;
import com.zygh.lz.entity.InformationUser;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.util.DataTime;
import com.zygh.lz.util.DateUtil;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InformationUserService {

    @Resource
    private InformationUserMapper informationUserMapper;
    @Resource
    private InformationMapper informationMapper;
    @Resource
    private StaffMapper staffMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public List<InformationUser> listAll() {
        return informationUserMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    public InformationUser getById(Integer id) {
        return informationUserMapper.getById(id);
    }

    /**
     * 新增，插入所有字段
     *
     * @param informationUser 新增的记录
     * @return 返回影响行数
     */
    public int insert(InformationUser informationUser) {
        return informationUserMapper.insert(informationUser);
    }

    /**
     * 新增，忽略null字段
     *
     * @param informationUser 新增的记录
     * @return 返回影响行数
     */
    public int insertIgnoreNull(InformationUser informationUser) {
        return informationUserMapper.insertIgnoreNull(informationUser);
    }

    /**
     * 修改，修改所有字段
     *
     * @param informationUser 修改的记录
     * @return 返回影响行数
     */
    public int update(InformationUser informationUser) {
        return informationUserMapper.update(informationUser);
    }

    /**
     * 修改，忽略null字段
     *
     * @param informationUser 修改的记录
     * @return 返回影响行数
     */
    public ResultBeans updateIgnoreNull(InformationUser informationUser) {
        return ResultUtil.resultBeansexecOp(informationUserMapper.updateIgnoreNull(informationUser), "修改");


    }

    /**
     * 删除记录
     *
     * @param informationUser 待删除的记录
     * @return 返回影响行数
     */
    public int delete(InformationUser informationUser) {
        return informationUserMapper.delete(informationUser);
    }

    public ResultBeans finalReading(String acceptWarningSignal) {
        return ResultUtil.resultBeansSuccess(informationUserMapper.finalReading(acceptWarningSignal));
    }

    public ResultBean findInforUserById(Integer id) {
        return ResultUtil.setOK("success", informationUserMapper.findInforUserById(id));
    }

    public ResultBean findInforUserByWarningSignal(String warningSignal, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map> inforUserByWarningSignal = informationUserMapper.findInforUserByWarningSignal(warningSignal, startTime, endTime);
        PageInfo<Map> pageInfo = new PageInfo<Map>(inforUserByWarningSignal);
        return ResultUtil.setOK("success", pageInfo);
    }

    public ResultBean schedulingLine(String warningSignal, String startDate, String endDate) throws ParseException {
        //默认取一周
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            startDate = DataTime.getStartDate();
            endDate = DataTime.getEndDate();
        }
        List startEnd = DateUtil.getStartEnd(startDate, endDate);
        String[] time = new String[startEnd.size()];
        int[] information = new int[startEnd.size()];
        for (int i = 0; i < startEnd.size(); i++) {
            List<Map> inforUserByWarningSignal = informationUserMapper.findInforUserByWarningSignal(warningSignal,
                    cn.hutool.core.date.DateUtil.beginOfDay(cn.hutool.core.date.DateUtil.parse(startEnd.get(i).toString())).toString(),
                    cn.hutool.core.date.DateUtil.endOfDay(cn.hutool.core.date.DateUtil.parse(startEnd.get(i).toString())).toString());
            time[i] = startEnd.get(i).toString();
            information[i] = inforUserByWarningSignal.size();
        }

        List<Map> list = new ArrayList<>();
        Map map = new HashMap();
        map.put("time", time);
        map.put("information", information);
        list.add(map);
        return ResultUtil.setOK("success", list);
    }


    public List<Map> schedulingInformation(int id) {
        Staff staff = staffMapper.selectByPrimaryKey(id);
        String code = staff.getStaffNum();
        if (StringUtils.isEmpty(code)) {
            return null;
        }
        String startDate = DataTime.getStartDate();
        String endDate = DataTime.getEndDate();
        List<Information> list = informationMapper.selectList(Wrappers.<Information>lambdaQuery()
                .groupBy(Information::getMsgLevel));
        List<Map> mapList = new ArrayList<>();
        Map map = new HashMap();
        for (Information information : list) {
            //消息等级接受数
            List<Map> inforByCode = informationUserMapper.findInforByCode(code, information.getMsgLevel(), startDate, endDate, null);
            map.put(information.getMsgLevel(), inforByCode.size());
        }
        List<Map> inforByCodes = informationUserMapper.findInforByCode(code, null, startDate, endDate, 0);
        map.put("unread", inforByCodes.size());
        mapList.add(map);
        return mapList;

    }


}